﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Facebook2.WebUI.Facebook2WebService;
using Facebook2.WebUI.LocalDev;
using Facebook2.WebUI.AppCode;
using System.Reflection;
using System.Data.SqlClient;
namespace Facebook2.WebUI
{
    public partial class Default : Facebook2.WebUI.AppCode.Facebook2BaseClass
    {
      
        Table table = new Table();
        List<Control> controls = new List<Control>();

        protected void Page_Load(object sender, EventArgs e)
        {

            using (SqlConnection conn = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename='c:\users\jacob\documents\visual studio 2012\Projects\Facebook_2\FacebookWebService\App_Data\TfsDatabase.mdf';Integrated Security=True"))
            {
                string sql = "select [user_name] from dbo.[user] WHERE user_id = 1";
                SqlCommand sqlCommand = new SqlCommand(sql,conn);

                conn.Open();

                string name = sqlCommand.ExecuteScalar().ToString();

                Response.Write(name);
            }

            DatabaseWebControl[] databaseRecords = new DatabaseWebControl[3];

            databaseRecords[0] = new DatabaseWebControl
            {
                FieldName = "First_Name",
                DataType = "System.String",
                DisplayControl = "System.Web.UI.WebControls.TextBox, System.Web, Version=1.0.5000.0,Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a",
                DisplayProperty = "Text"
            };

            databaseRecords[1] = new DatabaseWebControl
            {
                FieldName = "First_Name_Label",
                DataType = "System.String",
                DisplayControl = "System.Web.UI.WebControls.Label, System.Web, Version=1.0.5000.0,Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a",
                DisplayProperty = "Text",
                DisplayValue = "First Name"
            };

            databaseRecords[2] = new DatabaseWebControl
            {
                FieldName = "Gender",
                DataType = "System.String",
                DisplayControl = "System.Web.UI.WebControls.Button, System.Web, Version=1.0.5000.0,Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a",
                DisplayProperty = "Text"
            };

            foreach (DatabaseWebControl r in databaseRecords)
            {
                Type controlType = Type.GetType(r.DisplayControl);
                PropertyInfo[] controlPropertiesArray = controlType.GetProperties();
                object control = Activator.CreateInstance(controlType);

                foreach (PropertyInfo controlProperty in controlPropertiesArray)
                {
                    if (controlProperty.Name == r.DisplayProperty)
                    {
                        // set the Control's property
                        controlProperty.SetValue(control, "data for this item", null);
                        controls.Add((Control)control);
                    }
                }
            }


            //Table table = new Table();
            //TableRow row = new TableRow();
            //TableRow row2 = new TableRow();
            //TableCell cell1 = new TableCell();
            //TableCell cell2 = new TableCell();
            //TableCell cell3 = new TableCell();
            //TableCell cell4 = new TableCell();
            //RadioButtonList radioList = new RadioButtonList();
            //ListItem radio1 = new ListItem();
            //ListItem radio2 = new ListItem();
            //ListItem radio3 = new ListItem();

            //table.Width = 800;
            //radio1.Text = "Hello";
            //radio2.Text = "Hi!";
            //radio3.Text = "Hi There!";
            //radioList.Items.Add(radio1);
            //radioList.Items.Add(radio2);
            //radioList.Items.Add(radio3);

            //cell1.Controls.Add(new TextBox());
            //cell2.Controls.Add(new TextBox());
            //cell3.Controls.Add(radioList);
            //cell4.Controls.Add(new TextBox());
            //row.Cells.Add(cell1);
            //row.Cells.Add(cell2);
            //table.Rows.Add(row);
            //row2.Cells.Add(cell3);
            //row2.Cells.Add(cell4);
            //table.Rows.Add(row2);
            //Section1Placeholder.Controls.Add(table);
            AddControlsToTable();
            Section1Placeholder.Controls.Add(table);
            Facebook2WebService.Service1Client service = new Facebook2WebService.Service1Client(webServiceEndpoint);
            string response = service.GetData(4);
            Response.Write(response);
        }

        private void AddControlsToTable()
        {
            int columnCounter = 1;
            TableRow row = new TableRow();
            Control last = controls.Last();
            foreach (Control control in controls)
            {
                if (columnCounter == 1)
                {
                    row = new TableRow();
                    columnCounter = 1;
                }

                TableCell cell = new TableCell();
                cell.Controls.Add(control);
                row.Cells.Add(cell);

                if (columnCounter == 2 || control.Equals(last))
                {
                    table.Rows.Add(row);
                    columnCounter = 1;
                }
                else
                {
                    columnCounter += 1;
                }
            }

        }
        private Type GetControlType(string typeLiteral)
        {

            return typeof(string);
        }
        //TableCell cell = new TableCell();
        //cell.Controls.Add((Control)control);
        //row.Cells.Add(cell);
        //table.Rows.Add(row);
    }
}